package com.qys.training.server.controller;

import com.qys.training.base.dto.BaseResult;
import com.qys.training.biz.file.dto.FileEditDTO;
import com.qys.training.biz.file.dto.FileFindRequestDTO;
import com.qys.training.biz.file.entity.FileEntity;
import com.qys.training.biz.file.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Winstone
 * @date 2020/8/4 - 3:20 下午
 */
@RestController
@RequestMapping("/file")
public class FileController {
    public static final Logger logger = LoggerFactory.getLogger(FileController.class);

    @Autowired
    FileService fileService;

    @PostMapping(path = "/upload")
    public @ResponseBody
    BaseResult upload(@RequestParam("file") MultipartFile file) {
        Long id = fileService.uploadFile(file);
        return BaseResult.success(id);
    }

    @PostMapping(path = "/update")
    public @ResponseBody
    BaseResult update(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) {
        Long res = fileService.updateFile(id, file);
        return BaseResult.success(res);
    }

    @GetMapping(path = "/download")
    public void download(@RequestParam("id") Long id, HttpServletResponse response) {
        fileService.downloadFile(id, response);
    }

    @GetMapping(path = "/delete")
    public @ResponseBody
    BaseResult delete(@RequestParam("id") Long id) {
        Long res = fileService.deleteFile(id);
        return BaseResult.success(res);
    }

    @PostMapping(path = "/find")
    public @ResponseBody
    BaseResult findPost(@RequestBody FileFindRequestDTO findRequestDTO) {
        List<FileEntity> fileEntityList = fileService.findFile(findRequestDTO);
        return BaseResult.success(fileEntityList);
    }

    @GetMapping(path = "/find")
    public @ResponseBody
    BaseResult findGet(@RequestParam(name = "name", defaultValue = "") String name,
                       @RequestParam(name = "min") Long min,
                       @RequestParam(name = "max") Long max,
                       @RequestParam(name = "currentPage", defaultValue = "1") int currentPage,
                       @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        List<FileEntity> fileEntityList = fileService.findFile(new FileFindRequestDTO(name, min, max, currentPage, pageSize));
        return BaseResult.success(fileEntityList);
    }

    @GetMapping(path = "/downloadzip")
    public void downloadzip(@RequestParam(value = "ids") Long[] ids, HttpServletResponse response) {
        fileService.downloadZipFile(ids, response);
    }

    @PostMapping(path = "/edit")
    public @ResponseBody
    BaseResult editPDF(@RequestBody FileEditDTO dto){
        Long id = fileService.addText(dto.getId(),dto.getText(),dto.getX(),dto.getY(),dto.getPageNum(),dto.getFont(),dto.getFontsize());
        return BaseResult.success(id);
    }


}
